package com.jiangjiang.eshop.cart.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.cart.domain.ShoppingCartItemEntity;
import com.jiangjiang.eshop.cart.mapper.ShoppingCartItemMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ShoppingCartItemService
 * @Description 购物车条目service
 * @Author jhw
 * @Date 2021/6/1 22:41
 * @Version 1.0
 **/

@Service
public class ShoppingCartItemService extends ServiceImpl<ShoppingCartItemMapper, ShoppingCartItemEntity> {

    /**
     * 根据商品sku id 查询购物车条目数据
     * @param shoppingCartId 购物车id
     * @param goodsSkuId 商品sku id
     * @return 购物车条目实体
     */
    public ShoppingCartItemEntity getShoppingCartItemByGoodsSkuId(Long shoppingCartId,Long goodsSkuId){
        LambdaQueryWrapper<ShoppingCartItemEntity> wrapper = Wrappers.<ShoppingCartItemEntity>lambdaQuery()
                .eq(ShoppingCartItemEntity::getShoppingCartId, shoppingCartId)
                .eq(ShoppingCartItemEntity::getGoodsSkuId, goodsSkuId);
        return this.getOne(wrapper);
    }

    /**
     * 根据购物车id 查询所有条目
     * @param shoppingCartId 购物车id
     * @return 购物车条目实体
     */
    public List<ShoppingCartItemEntity> getShoppingCartItemByCartId(Long shoppingCartId){
        LambdaQueryWrapper<ShoppingCartItemEntity> wrapper = Wrappers.<ShoppingCartItemEntity>lambdaQuery()
                .eq(ShoppingCartItemEntity::getShoppingCartId, shoppingCartId);
        return this.getBaseMapper().selectList(wrapper);
    }

    /**
     * 更新购物车条目数量
     * @param id 购物车条目id
     * @param purchaseQuantity 数量
     * @return 处理结果
     */
    public Boolean updatePurchaseQuantity(Long id, Long purchaseQuantity) {
        LambdaUpdateWrapper<ShoppingCartItemEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ShoppingCartItemEntity::getId, id)
                .set(ShoppingCartItemEntity::getPurchaseQuantity, purchaseQuantity);
        return this.update(wrapper);
    }
}
